package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.mapper.UserMapper;
import org.example.mode.RegisterUser;
import org.example.service.UerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserServiceImpl implements UerService {
    @Autowired
    UserMapper userMapper;

    @Override
    public String registerUser(RegisterUser registerUser) {
        log.info("用户注册：{}", registerUser);
        Integer userNum = userMapper.registerUser(registerUser);
        log.info("成功插入的数据条数{}", userNum);

        if (userNum > 0) return "数据插入成功";
        return "插入失败";
    }

    @Override
    public String login(RegisterUser registerUser) {
        String username = registerUser.getUsername();
        RegisterUser info = userMapper.getUserInfoByUserName(username);

        if (info == null) return "用户不存在";
        String password = registerUser.getPassword();
        if (!info.getPassword().equals(password)) return "密码错误";
        return  "登录成功";

    }

    @Override
    public String deleteUser(String username) {
        Integer deleteNum = userMapper.deleteuserByUsername(username);
        log.info("成功删除的数据条数:{}", deleteNum);

        if (deleteNum > 0) return "用户删除成功";
        return "用户删除失败，可能用户不存在";
    }

    @Override
    public String updateUser(RegisterUser registerUser) {
        Integer updateNum = userMapper.updateUserInfoUername(registerUser);
        log.info("成功更新的数据条数:{}", updateNum);

        if (updateNum > 0)return  "用户信息更新成功";
        return "用户信息更新失败,可能用户不存在";
    }
}